//
//  XZLeaveApplyDetailVC.m
//  Teecloud
//
//  Created by PC on 2018/11/7.
//  Copyright © 2018年 小志. All rights reserved.
//

#import "XZLeaveApplyDetailVC.h"
#import "XZLeaveInfoCell.h"
#import "XZLeaveInfoImgCell.h"
#import "XZLeaveInfoDynamicCell.h"
#import "XZLeaveInfo.h"
#import "XZAuditInfo.h"
@interface XZLeaveApplyDetailVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) XZLeaveInfo *model;
@property (nonatomic, strong) UITableView *tabView;

@end
#define LEAVEINFOCELL @"LeaveInfoCell"
#define LEAVEINFOCELLImg @"LEAVEINFOCELLImg"
#define LEAVEINFODynamic @"XZLeaveInfoDynamicCell"
@implementation XZLeaveApplyDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kWhiteColor;
    [self getLeaveInfo];
}
#pragma mark - Networking
- (void)getLeaveInfo{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValue:FROM_IOS forKey:@"from"];
    [dict setValue:[XGMArchiverTools cacheForKey:BUSIORGID] forKey:@"busiOrgId"];
    [dict setValue:[XGMArchiverTools cacheForKey:USER_UserId] forKey:@"userId"];
    [dict setValue:[XGMArchiverTools cacheForKey:USER_identifierNumber] forKey:@"loginFinger"];
    [dict setValue:self.id forKey:@"Id"];
    kWeakObject(self)
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [HJNetwork POSTWithURL:[NSString stringWithFormat:@"ajax-api.ashx?$c=TeeyunApp&$m=GetLeaveInfo&$v=%@",VERSION] parameters:dict cachePolicy:HJCachePolicyIgnoreCache callback:^(id responseObject, NSError *error) {
        [MBProgressHUD hideHUDForView:weakself.view animated:YES];
        if (!error) {
            if ([[responseObject objectForKey:RESULT_NAME_Success] boolValue]) {
                NSDictionary *dataInfo = [responseObject objectForKey:RESULT_NAME_Data];
                XZLeaveInfo *model = [XZLeaveInfo mj_objectWithKeyValues:dataInfo];
                weakself.model = model;
                //标题
                self.title = [NSString stringWithFormat:@"请假详情-%@",model.UserName];
                [weakself.tabView reloadData];
            }else{
                
                [MBProgressHUD showErrorOrSuc:[responseObject objectForKey:RESULT_NAME_Message] toView:weakself.view];
            }
            
        }else{
            [MBProgressHUD showErrorOrSuc:@"网络异常" toView:weakself.view];
        }
    }];
}
#pragma mark - TableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.model.ImgFileList.count > 0) {
        return 6;
    }
    return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    XZLeaveInfoCell *leaveInfoCell = [tableView dequeueReusableCellWithIdentifier:LEAVEINFOCELL];
    switch (indexPath.row) {
        case 0:
            leaveInfoCell.typeLabel.text = @"请假类型";
            leaveInfoCell.typeContentLabel.text = self.model.LeaveTypeName;
            break;
        case 1:
            leaveInfoCell.typeLabel.text = @"开始时间";
            leaveInfoCell.typeContentLabel.text = self.model.StartTime;
            break;
        case 2:
            leaveInfoCell.typeLabel.text = @"结束时间";
            leaveInfoCell.typeContentLabel.text = self.model.EndTime;
            break;
        case 3:
            leaveInfoCell.typeLabel.text = @"请假事由";
            leaveInfoCell.typeContentLabel.text = self.model.Memo;
            break;
        default:
            break;
    }
    if (self.model.ImgFileList.count > 0) {
        if (indexPath.row == 4) {
            XZLeaveInfoImgCell *imgCell = [tableView dequeueReusableCellWithIdentifier:LEAVEINFOCELLImg];
            imgCell.imgArray = self.model.ImgFileList;
            return imgCell;
        }else if (indexPath.row == 5){//进度
            XZLeaveInfoDynamicCell *dynamicCell = [tableView dequeueReusableCellWithIdentifier:LEAVEINFODynamic];
            dynamicCell.AuditInfo = self.model.AuditInfo;
            return dynamicCell;
        }
    }else{
        if (indexPath.row == 4) {
            XZLeaveInfoDynamicCell *dynamicCell = [tableView dequeueReusableCellWithIdentifier:LEAVEINFODynamic];
            dynamicCell.AuditInfo = self.model.AuditInfo;
            return dynamicCell;
        }
    }
    
    return leaveInfoCell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.model.ImgFileList.count > 0) {
        if (indexPath.row == 4) {
            return 80.0f;
        }else if (indexPath.row == 5){
            return self.model.AuditInfo.count *45 +10;
        }
    }else{
        if (indexPath.row == 4) {
            return self.model.AuditInfo.count *45 +10;
        }
    }
    
    if (indexPath.row == 3) {//请假事由
        //计算文字高度 加上10的间距
        CGFloat commentSize = [self getLabelHeightWithText:self.model.Memo width:kScreenWidth - 84 font:14.0f];
        return commentSize +20;
    }
    return 50.f;
}

/**
 *
 *计算文字高度
 */
- (CGFloat)getLabelHeightWithText:(NSString *)text width:(CGFloat)width font: (CGFloat)font{
    CGRect rect = [text boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:font]} context:nil];
    return rect.size.height;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:true];
}

- (UITableView *)tabView{
    if (!_tabView) {
        _tabView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, DEVICE_WIDTH, DEVICE_HEIGHT-kNavBarAndStatusBarHeight-kBottomSafeHeight) style:UITableViewStylePlain];
        _tabView.delegate = self;
        _tabView.dataSource = self;
        _tabView.showsVerticalScrollIndicator =false;
        _tabView.separatorStyle =UITableViewCellSeparatorStyleNone;
        [_tabView registerNib:[UINib nibWithNibName:NSStringFromClass([XZLeaveInfoCell class]) bundle:nil] forCellReuseIdentifier:LEAVEINFOCELL];
        //图片
        [_tabView registerNib:[UINib nibWithNibName:NSStringFromClass([XZLeaveInfoImgCell class]) bundle:nil] forCellReuseIdentifier:LEAVEINFOCELLImg];
        //动态
          [_tabView registerNib:[UINib nibWithNibName:NSStringFromClass([XZLeaveInfoDynamicCell class]) bundle:nil] forCellReuseIdentifier:LEAVEINFODynamic];
        
        [self.view addSubview:_tabView];
    }
    return _tabView;
}
@end
